package ch.usi.inf.pf2.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ch.usi.inf.pf2.editor.MultipleChangeCellColor;
import ch.usi.inf.pf2.gui.JSpreadSheetGUI;
import ch.usi.inf.pf2.gui.SpreadSheetTableModel;

/**
 * Listener for possible change of cells color.
 * 
 * @author Ebrisa Savina Mastrodicasa
 */

public class ChangeColorActionListener implements ActionListener {

	private JSpreadSheetGUI spreadTable;
	private Color newColor;

	/**
	 * Create a ChangeColorActionListener on a given table.
	 * 
	 * @param spreadTable The table where there are the cells to be colored.
	 * @param newColor The color the cells must be colored with.
	 */
	public ChangeColorActionListener(JSpreadSheetGUI spreadTable, Color newColor) {
		this.spreadTable = spreadTable;
		this.newColor = newColor;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		MultipleChangeCellColor mccc = new MultipleChangeCellColor(spreadTable
				.getSheet(), spreadTable.getSelectedRows(), spreadTable
				.getSelectedColumns(), newColor);
		mccc.evaluate();
		spreadTable.getStack().addActionToUndoStack(mccc);
		SpreadSheetTableModel tb = (SpreadSheetTableModel) spreadTable
				.getTable().getModel();
		tb.fireTableCellUpdated(spreadTable.getSelectedRow() + 1, spreadTable
				.getSelectedColumn() + 1);
		spreadTable.getTable().repaint();
		spreadTable.getSheet().setHasBeenSaved(false);
	}

}
